<?php
	class Hethong_model extends CI_Model
	{
		function BackupDuLieu() // đang hợp tác OK
		{
		
			// Load the DB utility class
			$this->load->dbutil();
			
			// Backup your entire database and assign it to a variable
			$prefs = array(
                'tables'      => array(),  // Array of tables to backup.
                'ignore'      => array(),           // List of tables to omit from the backup
                'format'      => 'txt',             // gzip, zip, txt
                'filename'    => 'mybackup.sql',    // File name - NEEDED ONLY WITH ZIP FILES
                'add_drop'    => FALSE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );

			$backup =& $this->dbutil->backup($prefs);
			
			// Load the file helper and write the file to your server
			
			$filename = date('Y_m_d_H_i_s');
			$dir = 'application/backup/' . $filename . '.txt';
			$this->load->helper('file');
			$query = write_file($dir, $backup); 
			if($query)
				return true;
			else
				return NULL;
			
			
			// Load the download helper and send the file to your desktop
			//$this->load->helper('download');
			//force_download('mybackup.gz', $backup);
		}
		
		function XoaFile($tenfile)
		{
			$return = unlink("application/backup/".$tenfile);
			return $return;
		}
		function download($tenfile)
		{
			$this->load->helper('download');
			$data = file_get_contents('application/backup/'.$tenfile);
			$return = force_download($tenfile, $data);
			return $return;
		}
		
		/*function KhoiPhucDuLieu($tenfile)
		{
			$this->db->query("DROP DATABASE i12db");
			$this->db->query("CREATE DATABASE `i12db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;");
			$this->db->query("USE `i12db`");
			$file_content = '';
			$file = fopen("application/backup/".$tenfile,"r");
			while(!feof($file))
			{
				$temp = fgets($file);
				if(!strstr($temp,"#"))
					$file_content .= $temp;
			}
			fclose($file);
			$query_list = explode(";", $file_content);
			$chitietphieudat = array();
			foreach($query_list as $q)
			{
				if($q != "")
					$this->db->query($q);
								
			}
			return true;
		}*/
	}
?>